package com.feng.vhr.controller;

import com.feng.vhr.service.IHrService;
import com.feng.vhr.utils.VerificationCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.io.IOException;

@RestController
public class LoginController {
    @Autowired
    private IHrService hrService;

    /**
     * 用户登录
     * @param map
     * @param session
     * @return
     */
    /*@PostMapping("/doLogin")
    public RespBean login(@RequestBody Map<String,String> map, HttpSession session) {
        String verify_code = (String) session.getAttribute("verify_code");
        QueryWrapper<Hr> wrapper = new QueryWrapper<>();
        wrapper.eq("username", map.get("username"));
        Hr hr = hrService.getOne(wrapper);
        String pwd = MD5.encrypt(map.get("password"));
        if (hr != null && pwd.equals(hr.getPassword())) {
            if (verify_code.equalsIgnoreCase(map.get("code"))) {
                session.setAttribute("user",hr);
                return RespBean.ok("登陆成功", hr);
            } else {
                return RespBean.error("验证码错误！");
            }
        }
        return RespBean.error("该用户不存在！");
    }*/

    /**
     * 注销登录
     * @param session
     * @return
     */
   /* @GetMapping("/logout")
    public RespBean logout(HttpSession session){
        session.removeAttribute("user");
        return RespBean.ok("退出成功");
    }*/


    /**
     * 验证码写出
     * @param request
     * @param resp
     * @throws IOException
     */
    @GetMapping("/verifyCode")
    public void verifyCode(HttpServletRequest request, HttpServletResponse resp) throws IOException {
        VerificationCode code = new VerificationCode();
        BufferedImage image = code.getImage();
        String text = code.getText();
        HttpSession session = request.getSession(true);
        session.setAttribute("verify_code", text);
        try {
            VerificationCode.output(image, resp.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
